package com.hlingsoft.kkh.action.admin;

import com.hlingsoft.kkh.dao.RewardsDao;
import com.hlingsoft.kkh.entity.Rewards;
import com.hlingsoft.kkh.entity.vo.JsonVo;
import com.hlingsoft.kkh.entity.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin/rewards")
public class AdminRewardsAction extends AdminBaseAction {

	@Autowired
	private RewardsDao rewardsDao;

	@RequestMapping(value = "/page.htm", method = RequestMethod.GET)
	public String allList(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap modelMap) {

		PageVo<Rewards> pageVo = new PageVo<Rewards>(pageNum);
		pageVo.setRows(20);
		List<Rewards> list = rewardsDao.getRewardsList(pageVo.getOffset(),
				pageVo.getRows());
		pageVo.setList(list);
		pageVo.setCount(rewardsDao.getRewardsListCount());
		modelMap.put("pageVo", pageVo);
		return "system/rewards/all";
	}

	@RequestMapping(value = "/toAdd.htm", method = RequestMethod.GET)
	public String toAdd(HttpServletRequest request, ModelMap modelMap) {
		modelMap.put("kindId", System.currentTimeMillis());
		modelMap.put("JSESSIONID", request.getSession().getId());
		return "system/rewards/add";
	}

	@RequestMapping(value = "/delete.json")
	@ResponseBody
	public JsonVo<String> delete(
			@RequestParam(value = "rewardsId") long rewardsId, ModelMap modelMap) {
		JsonVo<String> json = new JsonVo<String>();
		rewardsDao.deleteRewards(rewardsId);
		json.setResult(true);
		return json;
	}

	@RequestMapping(value = "/add.json")
	@ResponseBody
	public JsonVo<String> add(@RequestParam(value = "url") String url,
			@RequestParam(value = "logo") String logo,
			@RequestParam(value = "rate") Double rate, ModelMap modelMap) {
		JsonVo<String> json = new JsonVo<String>();
		json.setResult(true);
		Rewards rewards = new Rewards();
		rewards.setUrl(url);
		rewards.setLogo(logo);
		rewards.setRate(rate);
		rewardsDao.addRewards(rewards);
		return json;
	}

}
